package com.ysd.common.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

@Configuration
public class RedisConfig {

    //注入jedisPool的连接池
    @Bean(name = "jedis.pool")

    @Autowired
    public JedisPool jedisPool(@Qualifier("jedis.pool.config")JedisPoolConfig config, @Value("${jedis.pool.host}")String host,@Value("${jedis.pool.port}")int port){
        return new JedisPool(config,host,port);
    }

    @Bean(name = "jedis.pool.config")
    public JedisPoolConfig jedisPoolConfig(
            @Value("${jedis.pool.config.maxTotal}")int maxTotal,
            @Value("${jedis.pool.config.maxIdle}")int maxIdle,
            @Value("${jedis.pool.config.minIdle}")int minIdle,
            @Value("${jedis.pool.config.maxWaitMillis}")int maxWaitMillis){
       JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
       jedisPoolConfig.setMaxTotal(maxTotal);
       jedisPoolConfig.setMaxIdle(maxIdle);
       jedisPoolConfig.setMinIdle(minIdle);
       jedisPoolConfig.setMaxWaitMillis(maxWaitMillis);
       return jedisPoolConfig;
    }
}
